package org.github.socket.protocol.ykq.server.process;

import cn.hutool.core.util.ByteUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.NumberUtil;
import org.github.socket.protocol.ykq.protocol.XPacket;
import org.github.socket.protocol.ykq.util.Cp56Time2aUtil;
import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.transport.AioSession;
import org.tinylog.Logger;

import java.nio.ByteOrder;
import java.util.Arrays;

/**
 * 0x09
 * 充电桩计费模型与平台不一致时，都需要请求计费模型，如计费模型请求不成功，则禁止充电
 */
public class OrderOverStateProcessor implements MessageProcessor<XPacket> {
    /**
     * 处理接收到的消息
     *
     * @param session 通信会话
     * @param packet  待处理的业务消息
     */
    @Override
    public void process(AioSession session, XPacket packet) {
        byte[] msg = packet.getMsg();
        Logger.info("=====================================交易记录结束协议开始===============================================");
        byte[] transactions = Arrays.copyOfRange(msg, 0, 16); //16位
        String transactionsStr = HexUtil.encodeHexStr(transactions);
        Logger.info("交易流水号 {}", transactionsStr);

        byte[] chargingId = Arrays.copyOfRange(msg, 16, 23); //7位
        String chargingIdStr = HexUtil.encodeHexStr(chargingId);
        Logger.info("桩编码 {}", chargingIdStr);


        byte[] chargingNumber = Arrays.copyOfRange(msg, 23, 24); // 1位
        Logger.info("枪号 %02X".formatted(chargingNumber[0] & 0xFF));


        byte[] startTime = Arrays.copyOfRange(msg, 24, 31); // 7位
        String startTimeStr = Cp56Time2aUtil.toDateString(startTime);
        Logger.info("开始时间{}",startTimeStr);

        byte[] endTime = Arrays.copyOfRange(msg, 31, 38); // 7位
        String endTimeStr = Cp56Time2aUtil.toDateString(endTime);
        Logger.info("结束时间{}",endTimeStr);

        byte[] b_jian_price = Arrays.copyOfRange(msg, 38, 42); // 4位
        double jian_price = NumberUtil.mul(ByteUtil.bytesToInt(b_jian_price, ByteOrder.BIG_ENDIAN), 0.00001);
        Logger.info("尖单价 {}",jian_price);

        byte[] b_jian_power = Arrays.copyOfRange(msg, 42, 46); // 4位
        double jian_power = NumberUtil.mul(ByteUtil.bytesToInt(b_jian_power, ByteOrder.BIG_ENDIAN), 0.00001);
        Logger.info("尖电量 {}",jian_power);

        byte[] b_jisun_jian_power = Arrays.copyOfRange(msg, 46, 50); // 4位
        double jisun_jian_power = NumberUtil.mul(ByteUtil.bytesToInt(b_jisun_jian_power, ByteOrder.BIG_ENDIAN), 0.00001);
        Logger.info("计损尖电量  {}",jisun_jian_power);

        byte[] b_jian_money = Arrays.copyOfRange(msg, 50, 54); // 4位
        double jian_money = NumberUtil.mul(ByteUtil.bytesToInt(b_jian_money, ByteOrder.BIG_ENDIAN), 0.00001);






        Logger.info("尖金额  {}",jian_money);
        Logger.info("=====================================交易记录结束协议结束===============================================");

    }
}
